<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <TableFilter />
        <ActionBar v-if="!forSelection" />
      </div>
      <PageableTable
        ref="pagetable"
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        :multiple-selected="forSelection"
        class="fixed-table-table"
        @page-change="onPageChange"
        @sort-change="onSortChange"
        @selection-change="onSelectionChange"
      >
        <el-table-column v-for="column in insideColumns" :key="column.key" :label="column.title" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column v-if="!forSelection" label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="text" title="编辑" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button type="text" title="删除" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
          </template>
        </el-table-column>
      </PageableTable>
      <FormDialog title="编辑" :show.sync="dialogFormVisible" width="40%" @on-ok="ok" @on-cancel="cancel">
        <EditForm :id="editId" ref="editForm" />
      </FormDialog>
    </div>
  </div>
</template>

<script>
// secondary package based on el-pagination
import FormDialog from '@neu/biz-core/components/FormDialog'
// 表格查询组件独立编写，需要传递pageable-table实例的mixin方法filter
import TableFilter from './components/tableFilter'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './components/editForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'
import databind from '@neu/biz-core/mixins/databind'
import { formatDate } from '@neu/biz-core/utils/utils'

import {
  page,
  remove
} from '@neu/dcam-ui/api/stock'
import { param } from '@neu/biz-core/utils'
export default {
  name: 'StockPage',
  components: {
    TableFilter,
    FormDialog,
    EditForm,
    ActionBar,
    PageableTable
  },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  props: {
    forSelection: {
      type: Boolean,
      default: false
    },
    status: {
      type: String,
      default: ''
    },
    selectRows: {
      type: Array,
      default: null
    }
  },
  data() {
    return {
      r: page,
      d: remove,
      insideColumns: [
        {
          title: '货品编号',
          key: 'storckNumber'
        }, {
          title: '备品备件分类',
          key: 'spareTypeName'
        },
        {
          title: '规格型号',
          key: 'specification'
        },
        {
          title: 'SN',
          key: 'sn'
        },
        {
          title: '存放库房',
          key: 'storehouseName'
        },
        {
          title: '单价',
          key: 'unitPrice'
        },
        {
          title: '货品来源',
          key: 'source',
          render: (h, params) => {
            let sourceDesc = ''
            if (params.row.source === 'new') {
              sourceDesc = '合同采购'
            } else if (params.row.source === 'back') {
              sourceDesc = '退库利旧'
            }
            return h('span', [
              h('el-tag', sourceDesc)
            ])
          }
        },
        {
          title: '关联合同',
          key: 'contractName'
        },
        {
          title: '状态',
          key: 'status',
          render: (h, params) => {
            if (params.row.status === 'used') {
              return '已使用'
            } else if (params.row.status === 'lock') {
              return '已分配'
            } else {
              return '闲置'
            }
          }
        },
        {
          title: '创建时间',
          key: 'createTime',
          render: (h, params) => { return h('span', formatDate(params.row.createTime)) }
        }
      ],
      dialogFormVisible: false,
      textMap: {
        update: '编辑',
        create: '新建'
      },
      dialogStatus: 'create',
      editId: -1
    }
  },
  created() {
    const params = {
      's_status': this.status
    }
    this.filter(params)
    // eslint-disable-next-line no-undef
    if (this.selectRows) {
      this.selectRows.forEach(row => {
        if (this.insideTableData.indexOf(row.id) >= 0) {
          this.$refs.pageableTable.toggleRowSelection(row, true)
        }
      })
    }
  },
  methods: {
    init(rows) {
      if (rows) {
        rows.forEach(row => {
          if (this.insideTableData.indexOf(row.stock) >= 0) {
            this.$refs.pagetable.$refs.pageableTable.toggleRowSelection(row.stock, true)
          }
        })
      }
    },
    // 表格编辑按钮点击事件处理
    edit(id) {
      // 打开对话框
      this.dialogFormVisible = true
      // 更新当前被编辑实体的ID，自动传递至编辑表单
      this.editId = id
    },
    // 编辑资产对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      // 关闭对话框
      this.dialogFormVisible = false
    }
  }
}
</script>
